MODULE SUSSOIL_MOD
CONTAINS
SUBROUTINE SUSSOIL(PTHRFRTI,LD_LEVGEN,LD_LESSRO,LD_LESN09,LD_LESNML,LD_LESNICE,PNSNMLWS,&
 & YDDIM,YDCST,YDSOIL,PRALFMINPSN,PRCIMIN)
USE PARKIND1  , ONLY : JPIM, JPRB, JPRD
USE YOMHOOK   , ONLY : LHOOK, DR_HOOK, JPHOOK
USE YOS_DIM   , ONLY : TDIM, JPTEXT
USE YOS_CST   , ONLY : TCST
USE YOS_SOIL  , ONLY : TSOIL
USE CPTAVE_MOD

! (C) Copyright 1989- ECMWF.
!
! This software is licensed under the terms of the Apache Licence Version 2.0
! which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
! In applying this licence, ECMWF does not waive the privileges and immunities
! granted to it by virtue of its status as an intergovernmental organisation
! nor does it submit to any jurisdiction.

!**   *SUSSOIL* IS THE SET-UP ROUTINE FOR COMMON BLOCK *YOESOIL*

!     PURPOSE
!     -------
!          THIS ROUTINE INITIALIZES THE CONSTANTS IN COMMON BLOCK
!     *YOESOIL*

!     INTERFACE.
!     ----------
!     CALL *SUSURF* FROM *SUPHEC*

!     METHOD.
!     -------

!     EXTERNALS.
!     ----------
!        *CPTAVE* IS CALLED TO GET SOIL VALUES FOR EACH OF 3 BROAD
!                                     TEXTURAL CLASSES

!     REFERENCE.
!     ----------

!     Original    A.C.M. BELJAARS         E.C.M.W.F.      89/11/02

!     MODIFICATIONS
!     -------------
!     J.-J. MORCRETTE         E.C.M.W.F.      91/07/14
!     P. VITERBO              E.C.M.W.F.       8/10/93
!     P. Viterbo     99-03-26    Tiling of the land surface
!     C. Fischer 00-12-20 Meteo-France recode initialization of rdat to avoid
!                         memory overflow on SUN workstation
!     J.F. Estrade *ECMWF* 03-10-01 move in surf vob
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!     P. Viterbo    24-05-2004      Change surface units
!     P. Viterbo   ECMWF   03-12-2004  Include user-defined RTHRFRTI
!     G. Balsamo   ECMWF   08-01-2006  Include Van Genuchten Hydro.
!     G. Balsamo   ECMWF   11-01-2006  Include sub-grid scale runoff
!     E. Dutra             12-11.2008  Include new snow parameterization
!     E. Dutra             16-11-2009  snow 2009 cleaning
!     G. Balsamo           21-12-2009  snow albedo retuning after spectral fix
!      F. Vana  05-Mar-2015  Support for single precision
!     G. Arduini           17-01-2019  snowML wind-induced densification 
!     R. Hogan             26-02-2019  Revert "compensation for albedo correction" (led to snow overestimate)
!     R. Hogan             07-03-2019  Permanent-snow albedo RALFMINPSN no longer hard coded
!     I. Ayan-Miguez (BSC) Oct 2023:   Move derived spatially distributed parameters to surf/module/susdp_deriv_ctl_mod.F90 routine


!     ------------------------------------------------------------------


IMPLICIT NONE

! Declaration of arguments

REAL(KIND=JPRB)   ,INTENT(IN)    :: PTHRFRTI
LOGICAL           ,INTENT(IN)    :: LD_LEVGEN
LOGICAL           ,INTENT(IN)    :: LD_LESSRO
LOGICAL           ,INTENT(IN)    :: LD_LESN09
LOGICAL           ,INTENT(IN)    :: LD_LESNML
LOGICAL           ,INTENT(IN)    :: LD_LESNICE
INTEGER(KIND=JPIM),INTENT(IN)    :: PNSNMLWS
TYPE(TDIM)        ,INTENT(IN)    :: YDDIM
TYPE(TCST)        ,INTENT(IN)    :: YDCST
TYPE(TSOIL)       ,INTENT(INOUT) :: YDSOIL
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRALFMINPSN
REAL(KIND=JPRB)   ,INTENT(IN), OPTIONAL :: PRCIMIN

REAL(KIND=JPRB) :: ZTHECAP(JPTEXT),ZTHEPWP(JPTEXT),ZTHESAT(JPTEXT),&
 & ZWB(JPTEXT),ZWCONS(JPTEXT),ZWPSIS(JPTEXT)  
REAL(KIND=JPRB), ALLOCATABLE :: ZMVGALPHA(:),ZWCONSM(:),ZNFAC(:),&
 & ZLAMBDA(:),ZWSATM(:),ZWRES(:)

REAL(KIND=JPRB) :: ZRCGDRY, ZRHOD, ZQ, ZLAMBDASM
INTEGER(KIND=JPIM) :: I, JS
REAL(KIND=JPHOOK) :: ZHOOK_HANDLE

!     ------------------------------------------------------------------

!*         1.     SET CONSTANTS
!                 -------------

IF (LHOOK) CALL DR_HOOK('SUSSOIL_MOD:SUSSOIL',0,ZHOOK_HANDLE)
ASSOCIATE(RPI=>YDCST%RPI, RTT=>YDCST%RTT, &
 & NCSS=>YDDIM%NCSS,NCSNEC=>YDDIM%NCSNEC, &
 & LESN09=>YDSOIL%LESN09, LESNML=>YDSOIL%LESNML, LESNICE=>YDSOIL%LESNICE, &
 & NLEVSN=>YDSOIL%NLEVSN, LESSRO=>YDSOIL%LESSRO, LEVGEN=>YDSOIL%LEVGEN, &
 & NSOTY=>YDSOIL%NSOTY, RALFMINPSN=>YDSOIL%RALFMINPSN, & 
 & RCIMIN=>YDSOIL%RCIMIN, RCONDSICE=>YDSOIL%RCONDSICE, RCWPSIS=>YDSOIL%RCWPSIS, &
 & RDFSICE=>YDSOIL%RDFSICE, RFRSMALL=>YDSOIL%RFRSMALL, &
 & RFRTINY=>YDSOIL%RFRTINY, RGH2O=>YDSOIL%RGH2O, RHOCI=>YDSOIL%RHOCI, &
 & RHOICE=>YDSOIL%RHOICE, RHOMAXSN=>YDSOIL%RHOMAXSN, &
 & RHOMAXSN_NEW=>YDSOIL%RHOMAXSN_NEW, RLAMBDADRY=>YDSOIL%RLAMBDADRY, &
 & RLAMICE=>YDSOIL%RLAMICE, RLAMSAT1=>YDSOIL%RLAMSAT1, &
 & RQSNCR=>YDSOIL%RQSNCR, RQWEVAP=>YDSOIL%RQWEVAP, RQWSBCR=>YDSOIL%RQWSBCR, &
 & RRCSICE=>YDSOIL%RRCSICE, RRCSOIL=>YDSOIL%RRCSOIL, &
 & RSIMP=>YDSOIL%RSIMP, RSNDKV=>YDSOIL%RSNDKV, RTF1=>YDSOIL%RTF1, &
 & RTF2=>YDSOIL%RTF2, RTF3=>YDSOIL%RTF3, RTF4=>YDSOIL%RTF4, &
 & RTFREEZSICE=>YDSOIL%RTFREEZSICE, RTHRFRTI=>YDSOIL%RTHRFRTI, &
 & RTMELTSICE=>YDSOIL%RTMELTSICE, RWB=>YDSOIL%RWB, RWCAP=>YDSOIL%RWCAP, &
 & RWCONS=>YDSOIL%RWCONS, RWPWP=>YDSOIL%RWPWP, RWSAT=>YDSOIL%RWSAT, NSNMLWS=>YDSOIL%NSNMLWS, &
 & RBARPWP=>YDSOIL%RBARPWP, RBARCAP=>YDSOIL%RBARCAP, &
 & RRHOSM=>YDSOIL%RRHOSM, RLAMBDAQ=>YDSOIL%RLAMBDAQ, RLAMBDAO=>YDSOIL%RLAMBDAO, &
 & RTFREEZSICECEL=>YDSOIL%RTFREEZSICECEL, RTMELTSICECEL=>YDSOIL%RTMELTSICECEL, &
 & RQSNCRINV=>YDSOIL%RQSNCRINV, RHOMINSN=>YDSOIL%RHOMINSN, RHODELTASN=>YDSOIL%RHODELTASN)

RGH2O  =4.18E6_JPRB
! Note: RQSNCRINV=15 before LESN09 implementation (LESN09=.false.)
RQSNCR =1.0_JPRB/RQSNCRINV

!     LAND SURFACE CONSTANTS

LEVGEN=LD_LEVGEN
LESSRO=LD_LESSRO

!    SNOW LOGICALS

LESN09=LD_LESN09
LESNML=LD_LESNML
LESNICE=LD_LESNICE ! activate snow over sea ice

! SNOW ML WARM START 
NSNMLWS=PNSNMLWS ! 1 initialize from own analysis
                 ! 2 initialize from single-layer (era5)
                 ! 3 initialize from offline land-surface ML5

! IF (LESN09) RQSNCR =1.0_JPRB/10._JPRB

!GPB IF(.NOT.LEVGEN) &

CALL CPTAVE(ZTHESAT,ZTHECAP,ZTHEPWP,ZWCONS,ZWPSIS,ZWB,RBARPWP,RBARCAP)

!     CONSTANTS DEFINING SOIL DISCRETIZATION

IF(.NOT.ALLOCATED(YDSOIL%RDAT)) ALLOCATE(YDSOIL%RDAT(NCSS))
IF(.NOT.ALLOCATED(YDSOIL%RDAW)) ALLOCATE(YDSOIL%RDAW(NCSS))
IF(.NOT.ALLOCATED(YDSOIL%RDAI)) ALLOCATE(YDSOIL%RDAI(NCSS))
!------------------------------------------------------------!
! 4-layers definition in cm (for a total depth of 289 cm)
!-----------!-------------!--------------!-------------------!
!thichness      mid-point    upper-bound     lower-bound
!-----------!-------------!--------------!-------------------!
!   7.0            3.5           0.0            7.0
!  21.0           17.5           7.0           28.0
!  72.0           64.0          28.0          100.0
! 189.0          194.5         100.0          289.0
!-----------!-------------!--------------!-------------------!
IF (NCSS >= 1) YDSOIL%RDAT(1)=0.07_JPRB
IF (NCSS >= 2) YDSOIL%RDAT(2)=0.21_JPRB
IF (NCSS >= 3) YDSOIL%RDAT(3)=0.72_JPRB
IF (NCSS >= 4) YDSOIL%RDAT(4)=1.89_JPRB
DO I=1,NCSS
  YDSOIL%RDAW(I)=YDSOIL%RDAT(I)
  YDSOIL%RDAI(I)=YDSOIL%RDAT(I)
ENDDO

!------------------------------------------------------------!
! 9-layers definition in cm (for a total depth of 300 cm)
!-----------!-------------!--------------!-------------------!
!thichness      mid-point    upper-bound     lower-bound
!-----------!-------------!--------------!-------------------!
!   1.0            0.5           0.0            1.0
!   2.0            2.5           1.0            3.0
!   4.0            4.5           3.0            7.0
!   8.0           11.0           7.0           15.0
!  10.0           16.0          15.0           25.0
!  25.0           37.5          25.0           50.0
!  50.0           75.0          50.0          100.0
! 100.0          150.0         100.0          200.0  
! 100.0          250.0         200.0          300.0
IF (NCSS == 9) THEN 
  YDSOIL%RDAT(1)=0.01_JPRB   
  YDSOIL%RDAT(2)=0.02_JPRB
  YDSOIL%RDAT(3)=0.04_JPRB
  YDSOIL%RDAT(4)=0.08_JPRB
  YDSOIL%RDAT(5)=0.10_JPRB
  YDSOIL%RDAT(6)=0.25_JPRB
  YDSOIL%RDAT(7)=0.50_JPRB
  YDSOIL%RDAT(8)=1.00_JPRB
  YDSOIL%RDAT(9)=1.00_JPRB
  DO I=1,NCSS
    YDSOIL%RDAW(I)=YDSOIL%RDAT(I)
    YDSOIL%RDAI(I)=MIN(YDSOIL%RDAT(I),0.25_JPRB)
  ENDDO
ENDIF


!-----------------------------------------------------------------!
! 10-layers definition in cm (for a total depth of 800 cm)
!-----------!---------------!-----------------!-------------------!
!layer   thichness      mid-point    upper-bound     lower-bound
!-----!----------!-------------!--------------!-------------------!
! 1      1.0            0.5           0.0            1.0
! 2      2.0            2.0           1.0            3.0
! 3      4.0            5.0           3.0            7.0
! 4      9.0           11.5           7.0           16.0
! 5     12.0           22.0          16.0           28.0
! 6     30.0           43.0          28.0           58.0
! 7     42.0           79.0          58.0          100.0
! 8    100.0          150.0         100.0          200.0                         
! 9    200.0          300.0         200.0          400.0
!10    400.0          600.0         400.0          800.0
!-----!----------!-------------!--------------!-------------------!

IF (NCSS == 10) THEN 
  YDSOIL%RDAT(1)=0.01_JPRB   
  YDSOIL%RDAT(2)=0.02_JPRB
  YDSOIL%RDAT(3)=0.04_JPRB
  YDSOIL%RDAT(4)=0.09_JPRB
  YDSOIL%RDAT(5)=0.12_JPRB
  YDSOIL%RDAT(6)=0.30_JPRB
  YDSOIL%RDAT(7)=0.42_JPRB
  YDSOIL%RDAT(8)=1.00_JPRB
  YDSOIL%RDAT(9)=2.00_JPRB
  YDSOIL%RDAT(10)=4.00_JPRB
  DO I=1,NCSS
    YDSOIL%RDAW(I)=YDSOIL%RDAT(I)
    YDSOIL%RDAI(I)=MIN(YDSOIL%RDAT(I),0.25_JPRB)
  ENDDO
ENDIF
!     CONSTANTS FOR HYDRAULIC DIFFUSIVITY AND HYDRAULIC CONDUCTIVITY
!GPB IF (LEVGEN) THEN
! VAN GENUCHTEN (MV) HYDROLOGY
!
!     COMPUTE AVERAGE SOIL PROPERTIES FOR SOIL TYPES
!
!     SOIL TYPE 1 (COARSE)
!     SOIL TYPE 2 (MEDIUM)
!     SOIL TYPE 3 (MEDIUM FINE)
!     SOIL TYPE 4 (FINE)
!     SOIL TYPE 5 (VERY FINE)
!     SOIL TYPE 6 (EXT.TROP. ORGANIC SOIL)
!     SOIL TYPE 7 (TROP. ORGANIC SOIL)

  NSOTY=7
  IF(.NOT.ALLOCATED(ZMVGALPHA)) ALLOCATE (ZMVGALPHA(0:NSOTY))
  IF(.NOT.ALLOCATED(ZWCONSM)) ALLOCATE (ZWCONSM(0:NSOTY))
  IF(.NOT.ALLOCATED(ZNFAC)) ALLOCATE (ZNFAC(0:NSOTY))
  IF(.NOT.ALLOCATED(ZLAMBDA)) ALLOCATE (ZLAMBDA(0:NSOTY))
  IF(.NOT.ALLOCATED(ZWSATM)) ALLOCATE (ZWSATM(0:NSOTY))
  IF(.NOT.ALLOCATED(ZWRES)) ALLOCATE (ZWRES(0:NSOTY))
  IF(.NOT.ALLOCATED(YDSOIL%RMVGALPHA)) ALLOCATE (YDSOIL%RMVGALPHA(0:NSOTY))
  IF(.NOT.ALLOCATED(YDSOIL%RWCONSM)) ALLOCATE (YDSOIL%RWCONSM(0:NSOTY))
  IF(.NOT.ALLOCATED(YDSOIL%RNFACM)) ALLOCATE (YDSOIL%RNFACM(0:NSOTY))
  IF(.NOT.ALLOCATED(YDSOIL%RLAMBDAM)) ALLOCATE (YDSOIL%RLAMBDAM(0:NSOTY))
  IF(.NOT.ALLOCATED(YDSOIL%RWSATM)) ALLOCATE (YDSOIL%RWSATM(0:NSOTY))
  IF(.NOT.ALLOCATED(YDSOIL%RWRESTM)) ALLOCATE (YDSOIL%RWRESTM(0:NSOTY))

  ZMVGALPHA(1:NSOTY)=(/3.83_JPRB,3.14_JPRB,0.83_JPRB,3.67_JPRB,2.65_JPRB,1.300_JPRB,3.14_JPRB/)
  ZWCONSM(1:NSOTY)=(/6.94E-6_JPRB,1.16E-6_JPRB,2.63E-7_JPRB,2.87E-6_JPRB,&
 &             1.740E-6_JPRB, 0.93E-6_JPRB,1.16E-6_JPRB/)
  ZNFAC(1:NSOTY)=(/1.3774_JPRB,1.1804_JPRB,1.2539_JPRB,1.1012_JPRB,1.1033_JPRB,1.2039_JPRB,1.1804_JPRB/)
  ZLAMBDA(1:NSOTY)=(/1.250_JPRB,-2.3421_JPRB,-0.5884_JPRB,-1.9772_JPRB,2.5000_JPRB,&
 &             0.40000_JPRB,-2.3421_JPRB/)
  ZWSATM(1:NSOTY)=(/0.403_JPRB,0.439_JPRB,0.430_JPRB,0.520_JPRB,0.614_JPRB,0.766_JPRB,0.439_JPRB/)
  ZWRES(1:NSOTY)=(/0.025_JPRB,0.010_JPRB,0.010_JPRB,0.010_JPRB,0.010_JPRB,0.010_JPRB,0.010_JPRB/)

! Set 0 SOILTYPE (WATER) VALUES TO 0
  ZMVGALPHA(0)=0.0_JPRB
  ZWCONSM(0)=0.0_JPRB
  ZNFAC(0)=0.0_JPRB
  ZLAMBDA(0)=0.0_JPRB
  ZWSATM(0)=0.0_JPRB
  ZWRES(0)=0.0_JPRB
  YDSOIL%RMVGALPHA(0)=0.0_JPRB
  YDSOIL%RWCONSM(0)=0.0_JPRB
  YDSOIL%RNFACM(0)=0.0_JPRB
  YDSOIL%RLAMBDAM(0)=0.0_JPRB
  YDSOIL%RWSATM(0)=0.0_JPRB
  YDSOIL%RWRESTM(0)=0.0_JPRB

  DO JS=1,NSOTY
    YDSOIL%RMVGALPHA(JS)=ZMVGALPHA(JS)
    YDSOIL%RWCONSM(JS)=ZWCONSM(JS)
    YDSOIL%RLAMBDAM(JS)=ZLAMBDA(JS)
    YDSOIL%RNFACM(JS)=ZNFAC(JS)
    YDSOIL%RWSATM(JS)=ZWSATM(JS)
    YDSOIL%RWRESTM(JS)=ZWRES(JS)
  ENDDO
 
!GPB ELSE
! CLAPP AND HORNBERGER (CH) HYDROLOGY ON A LOAMY SOIL
!        MEDIUM SOIL TYPE VALUES ARE CHOSEN EVERYWHERE

  RWB=ZWB(2)
  RWCONS=ZWCONS(2)
  RCWPSIS=ZWPSIS(2)

!     CONSTANTS FOR DEFINING THE SOIL WATER HOLDING CHARACTERISTICS

!        MEDIUM SOIL TYPE VALUES ARE CHOSEN EVERYWHERE

  RWSAT=ZTHESAT(2)
  RWCAP=ZTHECAP(2)
  RWPWP=ZTHEPWP(2)

  RQWEVAP=1.0_JPRB/(RWCAP-RWPWP)
  RQWSBCR=RPI/(RWCAP*1.6_JPRB)
!GPB ENDIF
!     CONSTANTS FOR THERMAL DIFFUSIVITY

!     VALUES TAKEN FROM Peters-Liddard et al. 1998
!     COMMON TO CH AND MV
ZQ=0.4_JPRB
ZLAMBDASM=(RLAMBDAQ**ZQ)*(RLAMBDAO**(1.0_JPRB-ZQ))
ZRCGDRY=1.6E6_JPRB

!GPB IF(LEVGEN)THEN

!     VAN GENUCHTEN 

!  IF(.NOT.ALLOCATED(YDSOIL%RLAMSAT1M)) ALLOCATE (YDSOIL%RLAMSAT1M(NSOTY))
!  IF(.NOT.ALLOCATED(YDSOIL%RRCSOILM)) ALLOCATE (YDSOIL%RRCSOILM(NSOTY))
!  DO JS=1,NSOTY
!    ZRHOD=(1.0_JPRB-YDSOIL%RWSATM(JS))*ZRHOSM
!    YDSOIL%RLAMBDADRYM(JS)=(0.135_JPRB*ZRHOD+64.7_JPRB)/(ZRHOSM-0.947*ZRHOD)
!    YDSOIL%RLAMSAT1M(JS)=ZLAMBDASM**(1.0_JPRB-YDSOIL%RWSATM(JS))
!     SOIL HEAT CAPACITY

!    YDSOIL%RRCSOILM(JS)=(1.0_JPRB-YDSOIL%RWSATM(JS))*ZRCGDRY+YDSOIL%RWCAPM(JS)*RGH2O
!  ENDDO
!GPB ELSE

!     CLAPP-HORNBERGER

  ZRHOD=(1.0_JPRB-RWSAT)*RRHOSM
  RLAMBDADRY=(0.135_JPRB*ZRHOD+64.7_JPRB)/(RRHOSM-0.947_JPRB*ZRHOD)
  RLAMSAT1=ZLAMBDASM**(1.0_JPRB-RWSAT)

!     SOIL HEAT CAPACITY

!     SET TO A CONSTANT REPRESENTATIVE OF THE VALUE AT FIELD CAPACITY

  RRCSOIL=(1-RWSAT)*ZRCGDRY+RWCAP*RGH2O

!GPB ENDIF

!     FORWARD TIMESTEP WEIGHTING FACTOR FOR TIME INTEGRATION

RSIMP=1._JPRB

!     CONSTANTS FOR SOIL WATER FREEZING

RTF3=0.5_JPRB*(RTF1+RTF2)
RTF4=RPI/(RTF1-RTF2)

!     SEA ICE RELATED CONSTANTS

RTFREEZSICE=RTT + RTFREEZSICECEL
RTMELTSICE=RTT + RTMELTSICECEL
RDFSICE=RCONDSICE/RRCSICE

IF (PRESENT(PRCIMIN)) THEN
  RCIMIN=PRCIMIN
ELSE
  RCIMIN=0.02_JPRB
ENDIF

!     SNOW RELATED CONSTANTS


RHOICE=920._JPRB
RLAMICE=2.22_JPRB
RHOCI=2.05E6_JPRB

! NUMBER of vertical layers in snowpack
NLEVSN=NCSNEC

! Albedo of permanent snow, used in Antarctica, Greenland and on
! glaciers. The value is set in su0phy.F90 and may be overriden in the
! NAMPHY namelist.
RALFMINPSN=PRALFMINPSN

! New snow density max value
! Value changed in the namelist
!RHOMAXSN_NEW=500._JPRB

RHOMAXSN=RHOMINSN + RHODELTASN ! rho max for glacier point
RFRTINY =1.E-7_JPRB
RFRSMALL=10._JPRB*RFRTINY

IF(.NOT.ALLOCATED(YDSOIL%RSNDTDESTC_ML)) ALLOCATE(YDSOIL%RSNDTDESTC_ML(NCSNEC))
IF (NCSNEC == 1) THEN
  YDSOIL%RSNDTDESTC_ML=(/0.046_JPRB/)   !  
ELSEIF(NCSNEC==5)THEN
  !*YDSOIL%RSNDTDESTC_ML=(/ 0.112_JPRB, 0.152_JPRB, 0.192_JPRB, 0.288_JPRB, 0.488_JPRB/)
  YDSOIL%RSNDTDESTC_ML=(/ 0.046_JPRB, 0.046_JPRB, 0.046_JPRB, 0.046_JPRB, 0.046_JPRB/)
ELSE
  YDSOIL%RSNDTDESTC_ML(1:NCSNEC)=0.046_JPRB
  YDSOIL%RSNDTDESTC_ML(1)=0.046_JPRB
ENDIF


IF(.NOT.ALLOCATED(YDSOIL%RLEVSNMIN)) ALLOCATE(YDSOIL%RLEVSNMIN(NCSNEC))
IF(.NOT.ALLOCATED(YDSOIL%RLEVSNMAX)) ALLOCATE(YDSOIL%RLEVSNMAX(NCSNEC))
IF(.NOT.ALLOCATED(YDSOIL%RLEVSNMIN_GL)) ALLOCATE(YDSOIL%RLEVSNMIN_GL(NCSNEC))
IF(.NOT.ALLOCATED(YDSOIL%RLEVSNMAX_GL)) ALLOCATE(YDSOIL%RLEVSNMAX_GL(NCSNEC))

! Defauls


IF (NCSNEC == 1) THEN
  YDSOIL%RLEVSNMIN=(/0.0_JPRB/)
  YDSOIL%RLEVSNMAX=(/-1._JPRB/)
  YDSOIL%RLEVSNMIN_GL=(/0.0_JPRB/)
  YDSOIL%RLEVSNMAX_GL=(/-1._JPRB/)
ELSEIF(NCSNEC==2) THEN
  YDSOIL%RLEVSNMIN=(/0.075_JPRB,0.05_JPRB/)
  YDSOIL%RLEVSNMAX=(/0.075_JPRB,-1._JPRB/)
  YDSOIL%RLEVSNMIN_GL=(/0.50_JPRB,0.50_JPRB/)
  YDSOIL%RLEVSNMAX_GL=(/0.50_JPRB,-1._JPRB/)
ELSEIF(NCSNEC==3) THEN
  YDSOIL%RLEVSNMIN=(/0.075_JPRB,0.05_JPRB,0.05_JPRB/)
  YDSOIL%RLEVSNMAX=(/0.075_JPRB,-1._JPRB,0.15_JPRB/)
  YDSOIL%RLEVSNMIN_GL=(/0.50_JPRB,0.50_JPRB,0.50_JPRB/)
  YDSOIL%RLEVSNMAX_GL=(/0.50_JPRB,0.50_JPRB,-1._JPRB/)
ELSEIF(NCSNEC==5) THEN
  YDSOIL%RLEVSNMIN=(/0.075_JPRB,0.05_JPRB,0.05_JPRB,0.05_JPRB,0.05_JPRB/)
  YDSOIL%RLEVSNMAX=(/0.075_JPRB,0.1_JPRB,0.2_JPRB,-1._JPRB,0.15_JPRB/)
  YDSOIL%RLEVSNMIN_GL=(/0.50_JPRB,0.50_JPRB,0.50_JPRB,0.50_JPRB,0.50_JPRB /)
  YDSOIL%RLEVSNMAX_GL=(/0.50_JPRB,0.50_JPRB,0.50_JPRB,0.50_JPRB,-1.0_JPRB /)
ELSE
  YDSOIL%RLEVSNMIN(1)=0.075_JPRB ! db fix
  YDSOIL%RLEVSNMIN(2:NCSNEC)=0.05_JPRB ! db fix
  YDSOIL%RLEVSNMAX(1:NCSNEC)=0.1_JPRB
  YDSOIL%RLEVSNMAX(1)=0.075_JPRB
  YDSOIL%RLEVSNMAX(NCSNEC-1)=-1._JPRB 

  YDSOIL%RLEVSNMIN_GL(1:NCSNEC)=0.50_JPRB
  YDSOIL%RLEVSNMAX_GL(1:NCSNEC)=0.50_JPRB
  YDSOIL%RLEVSNMAX_GL(NCSNEC)=-1._JPRB
ENDIF

! Tiles constants

RTHRFRTI=PTHRFRTI

YDSOIL%LESKTI5=.FALSE.
YDSOIL%LESKTI8=.FALSE.
YDSOIL%LESOILCOND=.TRUE.
YDSOIL%LEROLAKE=.FALSE.

!! Temporary Logicals 
!! Defaults 
YDSOIL%LEWBSOILFIX=.TRUE.
YDSOIL%LEWBCHECK=.FALSE.
YDSOIL%LEWBCHECKAbort=.FALSE.
YDSOIL%LESNCHECK=.FALSE.
YDSOIL%LESNCHECKAbort=.FALSE.
YDSOIL%LESNWBCON=.FALSE.

! To be better re-evaluated...
!!! default when snow multi-layer is active
!!IF ( LESNML ) THEN
!!  YDSOIL%LESKTI5=.TRUE.
!!ENDIF

END ASSOCIATE
IF (LHOOK) CALL DR_HOOK('SUSSOIL_MOD:SUSSOIL',1,ZHOOK_HANDLE)

!     ------------------------------------------------------------------

END SUBROUTINE SUSSOIL
END MODULE SUSSOIL_MOD
